package cn.onein.edu.web.controller;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.onein.edu.biz.bean.LearnContent;
import cn.onein.edu.util.DateUtil;
import cn.onein.edu.util.ResourceUtil;
import cn.onein.edu.web.common.base.BaseController;
import cn.onein.edu.web.service.LearnContentService;
import cn.onein.edu.web.vo.SampleDailyVO;

/*
 * 用户指南经典例子控制类
 */
@Controller
public class PromotionDailyController extends BaseController  {
	
	Logger logger = Logger.getLogger(PromotionDailyController.class);
	
	@Autowired
	private LearnContentService learnContentService;
	
	private final static int COUNT_KEY = 7;
	
	private final static int DATE_SUB_KEY = 3;
	
	private final static String ENCODI_KEY = "UTF-8";
	
	/**
	 * 用户指南浏览
	 * 
	 * @return
	 */
	@RequestMapping(value = "/promotion", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView dailyAction(HttpServletResponse resp, HttpServletRequest req)
			throws Exception {
		ResourceBundle dailyInfo = ResourceUtil.getDailyProperties();
		List<SampleDailyVO> vos = new ArrayList<SampleDailyVO>();
		Map<String,Object> param = new HashMap<String,Object>();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		List<LearnContent> contents = null;
		Date now = new Date();
		 
		try{
			for(int i = 1 ; i < COUNT_KEY ; i++){
				SampleDailyVO vo = new SampleDailyVO();
				vo.setId(String.valueOf(i));
				Date date = DateUtil.getDateBydays(now, i - DATE_SUB_KEY);
				String strDate = df.format(date);
				vo.setDate(strDate.substring(5, 10));
				vo.setWeek(DateUtil.getWeekOfDate(date));
				vo.setIntroText(convertUtf8(dailyInfo.getString("introText_" + String.valueOf(i))));
				vo.setScope(this.convertUtf8(dailyInfo.getString("scope_" + String.valueOf(i))));
				vos.add(vo);
			}
			
			contents = learnContentService.getHotContentByHits(7);
		}catch(Exception e){
			logger.error("Get hot content by hits error in promotion daily.", e);
			return new ModelAndView("/error",param);
		}
		
		param.put("dailys", vos);
		param.put("contents", contents);
		
		setMenu(param, MENU_VALUE_DAILY);
		return new ModelAndView("/promotion/dailyaction",param);
	}
	
	/**
	 * 用户指南详细
	 * 
	 * @return
	 */
	@RequestMapping(value = "/promotion/{id}", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView dailydetail(HttpServletResponse resp, HttpServletRequest req,@PathVariable String id)
			throws Exception {
		ResourceBundle dailyInfo = ResourceUtil.getDailyProperties();
		Map<String,Object> param = new HashMap<String,Object>();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		List<LearnContent> contents = null;
		String shareUrl = "";
		int prePage;
		int nextPage;
		int intId;
		
		SampleDailyVO vo = new SampleDailyVO();
		
		try{
			intId = Integer.parseInt(id);
			if(intId  < 1 || intId >= COUNT_KEY){
				return new ModelAndView("/resourceNotFound");
			}
			
			Date date = DateUtil.getDateBydays(new Date(), intId - DATE_SUB_KEY);
			String strDate = df.format(date);
			vo.setDate(strDate.substring(5, 10));
			vo.setWeek(DateUtil.getWeekOfDate(date));
			vo.setIntroText(this.convertUtf8(dailyInfo.getString("introText_" + id)));
			vo.setBabyDaily(this.convertUtf8(dailyInfo.getString("babyDaily_" + id)));
			vo.setDailyNote(this.convertUtf8(dailyInfo.getString("dailyNote_" + id)));
			vo.setFullText(this.convertUtf8(dailyInfo.getString("fullText_" + id)));
			
			contents = learnContentService.getHotContentByHits(7);
			
			if((prePage = intId - 1) < 1){
				prePage = intId;
			}
			
			if((nextPage = intId + 1) >= COUNT_KEY){
				nextPage = intId;
			}
		}catch(Exception e){
			logger.error("Get hot content by hits error in promotion daily.", e);
			return new ModelAndView("/resourceNotFound");
		}
		
		shareUrl = req.getRequestURL().toString();
		
		param.put("daily", vo);
		param.put("contents", contents); 
		param.put("shareUrl", shareUrl);
		
		param.put("prePage", prePage);
		param.put("nextPage", nextPage);
		param.put("currPage", intId);
		
		setMenu(param, MENU_VALUE_DAILY);
		return new ModelAndView("/promotion/dailydetail",param);
	}
	
	private String convertUtf8(String str) throws UnsupportedEncodingException{
		return new String(str.getBytes("ISO-8859-1"),ENCODI_KEY);
	}
}
